package cn.lut.mvc.handler;

import cn.lut.constant.CrowdConstant;
import cn.lut.entity.Admin;
import cn.lut.service.api.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @user:dell
 * @date:2020年11月23 13:44
 */
@Controller
public class AdminHandler {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "admin/update.html")
    public String update(Admin admin,@RequestParam("pageNum") Integer pageNum,@RequestParam("keyword") String keyword){
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }
    @RequestMapping(value = "admin/to/edit/page.html")
    public String toEditPage(
            @RequestParam("adminId") Integer adminId,ModelMap modelMap){
//        1.根据adminId查询对象
        Admin admin=  adminService.getAdminById(adminId);
//      2.将admin对象存入模型
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        return "admin-edit";
    }
    @RequestMapping(value = "admin/save.html")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }
    @RequestMapping(value = "/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(@PathVariable("adminId") Integer adminId,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("keyword") String keyword
    ){
//        执行删除
        adminService.remove(adminId);
//        页面的跳转,回到分页页面
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }
    @RequestMapping(value = "/admin/get/page.html")
    public String getPageInfo(
//            可以指定默认值，和sql语句配合，实现两种情况的适配
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
//            默认从第一页开始
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
//            每一页默认显示五条
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap){
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }
    @RequestMapping(value = "/admin/do/logout.html")
    public String doLogOut(HttpSession session){
//        强制session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    @RequestMapping(value = "/admin/do/login.html" )
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,HttpSession session){
        Admin admin= adminService.getAdminByLoginAcct(loginAcct,userPswd);
//           将登陆成功返回的admin对象存入session域

        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        System.out.println(userPswd);
        return "redirect:/admin/to/main/page.html";
    }
}

